/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package SessionBeans;


import entityBeans.Accounts;
import entityBeans.Products;
import entityBeans.Productsstore;
import exception.ExceptionCart;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.CreateException;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author BELALOUACHE
 */
@Stateful
public class Cart implements CartLocal {
    List<Products> lesProduits=new ArrayList();
    Double solde=0.0;
    enum EtatEnum {EMPTY, FILLING, BOUGHT}
    ;
    EtatEnum etat=EtatEnum.EMPTY;
    private static final String PERSISTENCE_UNIT_NAME = "StoreEcom-ejbPU";
    private EntityManagerFactory factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);

    @Override
    public double getTotalPrice() {
        return this.getSolde();
    }
   

    @Override
    public void setSolde(Double solde) {
        this.solde = solde;
    }

    @Override
    public Double getSolde() {
        return solde;
    }

    @Override
     public void setEtat(EtatEnum etat) {
        this.etat = etat;
    }

    @Override
    public EtatEnum getEtat() {
        return etat;
    }

    @Override
    public List<Products> getLesProduits() {
        return lesProduits;  
    }

    @Override
   public void setLesProduits(List<Products> lesProduits) {
        this.lesProduits = lesProduits;
    }
   
    @Override
    public CartLocal create() throws CreateException {
        return null;
    }

    @Override
    public void ejbCreate() throws CreateException {
        
    }
    @Override
    public void ejbRemove(){
       
    }

    @Override
    public void addProduct(int productPK) {
        EntityManager em = factory.createEntityManager();
        Products prod=null;
        try{
        if (this.getEtat()==EtatEnum.BOUGHT) {
        throw new ExceptionCart();
           }
        // Begin a new local transaction so that we can persist a new entity
        em.getTransaction().begin();
        prod = em.find(entityBeans.Products.class, productPK);
         // Commit the transaction, which will cause the entity to
	// be stored in the database
         this.getLesProduits().add(prod);
         this.setSolde(this.getSolde()+prod.getPriceProd());
         this.setEtat(EtatEnum.FILLING);
        }
        catch (ExceptionCart e) {
            e.getmessage1();
        }    
	// It is always good practice to close the EntityManager so that
	// resources are conserved.
	finally{em.close();}
    }
    
    @Override
    public void removeProduct(int productPK) {
        EntityManager em = factory.createEntityManager();
        boolean t=false;
        try{
        // Begin a new local transaction so that we can persist a new entity
	em.getTransaction().begin();
        Products prod = em.find(Products.class, productPK);
         // Commit the transaction, which will cause the entity to
	// be stored in the database
        for (int i=0; i<this.getLesProduits().size(); i++){
            if (this.getLesProduits().get(i)==prod) {
                t=true;
                break;
            }
         if (t=true){
             this.getLesProduits().remove(prod);
             this.setSolde(this.getSolde()-prod.getPriceProd());
             if (this.getLesProduits().isEmpty()) {
                 this.setEtat(EtatEnum.EMPTY);
             }
            }
         else {
                throw new ExceptionCart();
        }
            }
        }
        catch (ExceptionCart e) {
            e.getmessage3();
        }
	// It is always good practice to close the EntityManager so that
	// resources are conserved.
	finally{em.close();}
       }

    @Override
    public void buy(String accountId) {
        int excep=0;
        EntityManager em = factory.createEntityManager();
        try{
        if (EtatEnum.EMPTY.equals(getEtat())){
            excep=1;
            throw new ExceptionCart();
           }
       // Begin a new local transaction
        em.getTransaction().begin();
        Accounts cmpCust = em.find(Accounts.class, accountId);
        if (getTotalPrice()>cmpCust.getSolde()){
            excep=2;
            throw new ExceptionCart();
            }
       //Update Account customers
        cmpCust.setSolde(cmpCust.getSolde()-getTotalPrice());
        em.persist(cmpCust);
        // Update Account store
        Productsstore store = em.find(Productsstore.class, getLesProduits().get(0).getIdProductsstoreFk());
        Accounts cmpStore = em.find(Accounts.class, store.getIdAccountFk().getIdAccount());
        cmpStore.setSolde(cmpStore.getSolde()+getTotalPrice());
        em.persist(cmpStore);
        // Commit the transaction, which will cause the entity to
	// be stored in the database
        setEtat(EtatEnum.BOUGHT);
        }
        catch (ExceptionCart e) {
            if(excep==1) {
                e.getmessage2();
        }else if(excep==2){
                e.getmessage4();
        }
        }
	// It is always good practice to close the EntityManager so that
	// resources are conserved.
	finally{
            em.close();
        }
    }

    @Override
    public Cart getCart() {
        return this;
    }

    @Override
    public void clear(){
        for (int i=0; i<=lesProduits.size(); i++){
            lesProduits.remove(i);
        }
        solde=0.0;
        
    }

       
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
  
}
